package com.sloera.spring.aop.impl;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;

/**
 * @author sloera
 * @create 2020/4/8
 */
//将类声明为一个切面
@Aspect
@Component
@Order(1) //指定优先级。值小优先级高
public class LoggingAspect {

    /*
     * @Description 用于声明切入点表达式。一般不需要添加其他代码
     * 使用 @Pointcut 声明切入点表达式
     * @param :
     * @Return void
     * @Author SloeraN
     * @Date 2020/4/8 23:21
     */
    @Pointcut("execution(* com.sloera..*.impl.*.*(..)) || execution(* com.sloera..*.helloword.*.*(..))")
    public void declareJoinPointExpression() {

    }

    //声明该方法是一个前置通知：在目标方法执行之前执行
    //@Before("declareJoinPointExpression()")
    @Before("com.sloera.spring.aop.impl.LoggingAspect.declareJoinPointExpression()")
    public void beforeMethod(JoinPoint joinPoint) {
        String methodName = joinPoint.getSignature().getName();
        List<Object> args = Arrays.asList(joinPoint.getArgs());
        System.out.println("The method " + methodName + " begins with " + args);
    }

    //声明该方法是一个后置通知：在目标方法执行后执行。无论是否发生异常
    @After("declareJoinPointExpression()")
    public void afterMethod(JoinPoint joinPoint) {
        String methodName = joinPoint.getSignature().getName();
        List<Object> args = Arrays.asList(joinPoint.getArgs());
        System.out.println("The method " + methodName + " ends");
    }

    /*
     * @Description 声明该方法是一个返回通知：在目标方法正常执行后执行。
     * 可以访问到方法的返回值
     * @param joinPoint:
     * @Return void
     * @Author SloeraN
     * @Date 2020/4/8 22:20
     */
    @AfterReturning(value = "declareJoinPointExpression()", returning = "result")
    public void afterReturning(JoinPoint joinPoint, Object result) {
        String methodName = joinPoint.getSignature().getName();
        System.out.println("The method " + methodName + " ends with " + result);
    }

    /*
     * @Description 在目标方法出现异常时执行，可以访问到异常对象，且可以指定特定异常时执行（设置Exception具体类）
     * @param joinPoint:
     * @param ex:异常类型
     * @Return void
     * @Author SloeraN
     * @Date 2020/4/8 23:04
     */
    @AfterThrowing(value = "declareJoinPointExpression()", throwing = "ex")
    public void afterThrowing(JoinPoint joinPoint, Exception ex) {
        String methodName = joinPoint.getSignature().getName();
        System.out.println("The method " + methodName + " occurs exception " + ex.getMessage());
    }

    /*
     * @Description 环绕通知需要携带ProceedingJoinPoint类型的参数
     * 环绕通知类似于动态代理的全过程：ProceedingJoinPoint 类型的方法可以决定是否执行目标方法
     * 且环绕通知必须有返回值，作为目标方法的返回值
     * 会拦截异常通知
     * @param proceedingJoinPoint:
     * @Return void
     * @Author SloeraN
     * @Date 2020/4/8 23:07
     */
    @Around("declareJoinPointExpression()")
    public Object around(ProceedingJoinPoint proceedingJoinPoint) {

        Object result = null;
        String methodName = proceedingJoinPoint.getSignature().getName();
        //执行方法
        try {
            //前置通知
            System.out.println("The method in around " + methodName + " begins with " + Arrays.asList(proceedingJoinPoint.getArgs()));
            result = proceedingJoinPoint.proceed();
            System.out.println("The method in around " + methodName + " ends with " + result);
        } catch (Throwable throwable) {
            //异常通知
            System.out.println("The method in around " + methodName + " occurs exception " + throwable.getMessage());
            throwable.printStackTrace();
        }
        System.out.println("The method in around " + methodName + " ends");
        return result;
    }
}
